package cn.king.springframework.beans.factory;

import cn.king.springframework.beans.BeanException;
import cn.king.springframework.beans.PropertyValue;
import cn.king.springframework.beans.PropertyValues;
import cn.king.springframework.beans.factory.config.BeanFactoryPostProcessor;
import cn.king.springframework.core.io.DefaultResourceLoader;
import cn.king.springframework.core.io.Resource;
import cn.king.springframework.util.StringValueResolver;

import java.io.IOException;
import java.util.Properties;

public class PropertyPlaceholderConfigurer implements BeanFactoryPostProcessor {
    /**
     * Default placeholder prefix: {@value}
     */
    public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
    /**
     * Default placeholder suffix: {@value}
     */
    public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";
    private String location;
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeanException {
        // 加载属性文件
        try {
            DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
            Resource resource = resourceLoader.getResource(location);
            Properties properties = new Properties();
            properties.load(resource.getInputStream());
            String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
            for(String beanName:beanDefinitionNames){
                PropertyValues propertyValues = beanFactory.getBeanDefinition(beanName).getPropertyValues();
                for(PropertyValue propertyValue:propertyValues.getPropertyValues()){
                    Object value = propertyValue.getValue();
                    if(!(value instanceof String)){
                        continue;
                    }
                }
            }
            //向容器中添加字符串解析器,供供解析@Value注解使用
            StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(properties);
            beanFactory.addEmbeddedValueResolver(valueResolver);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setLocation(String location) {

        this.location = location;
    }

    private String resolvePlaceholder(String value, Properties properties){
        String strVal = value;
        StringBuilder buffer = new StringBuilder();
        int startIdx = strVal.indexOf(DEFAULT_PLACEHOLDER_PREFIX);
        int stopIdx = strVal.indexOf(DEFAULT_PLACEHOLDER_SUFFIX);
        if (startIdx != -1 && stopIdx != -1 && startIdx < stopIdx) {
            String propKey = strVal.substring(startIdx + 2, stopIdx);
            String propVal = properties.getProperty(propKey);
            buffer.replace(startIdx, stopIdx + 1, propVal);
        }
        return buffer.toString();
    }


private class PlaceholderResolvingStringValueResolver implements StringValueResolver{
    private final Properties properties;

    public PlaceholderResolvingStringValueResolver(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String resolveStringValue(String strValue) {
        return PropertyPlaceholderConfigurer.this.resolvePlaceholder(strValue,properties);
    }

}

}


